// pages/mine/anchorBind/anchorBind.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    anc_guilds: null,
    anc_quick: null,

    guild_id: null, //传回的数据
    table_flag: null,
    guild_name: null,
    detail: null,
    platfrom_id:null,

    anchors: null,
    bind_anchor: null,

    count: 1,

    loadName: "暂无更多",

    hidden:true,
    index:null,

    anchor_uid:'',

    role:null,




  },
  anchors_del:function(){
    let that=this;
    wx.showModal({
      content: '确定要解绑吗？',
      confirmColor:"#d0a76c",
      success:function(e){
        // console.log(e)
        if(that.data.role=="1"){
          //会长移除
          console.log(1)          
          wx.request({
            url: app.globalData.url + 'api/mine/guild/anchor/delete',
            method: "POST",
            data: {
              token: app.globalData.token,
              guild_id: that.data.guild_id,
              anchor_uid: that.data.anchor_uid,
            },
            success: function (e) {
              console.log(e)
            }
          })
        }else{
          //主播移除
          console.log(2)
          wx.request({
            url: app.globalData.url + 'api/mine/anchor/delete',
            method: "POST",
            data: {
              token: app.globalData.token,
              anchor_id: that.data.anchor_uid,
            },
            success: function (e) {
              console.log(e)
            }
          })
        }
        
      }
    })
  },
  wrapClick:function(e){
    this.setData({
      index: null,
    })
  },
  touchmove:function(e){
    // console.log(e)
    if(e.currentTarget.dataset.touch){
      this.setData({
        hidden:false,
        index: e.currentTarget.dataset.index,
        anchor_uid: e.currentTarget.dataset.anchor_uid
      })
    }
  },
  click_more: function(e) {
    if (this.data.platfrom_id) {
      //详情点击
      // console.log(132)
      wx.navigateTo({
        url: '../../bindingGuild/anchor/binding/binding?p_id=' + this.data.platfrom_id + '&&p_name=' + this.data.guild_name + '&&p_table_flag=' + this.data.table_flag,
      })
    } else {
      //主页点击
      wx.navigateTo({
        url: '../../bindingGuild/anchor/binding/binding',
      })
    }

  },
  click_detail: function(e) {
    console.log(e)
    let that = this;
    // console.log(e.currentTarget.dataset)
    let guild_id = e.currentTarget.dataset.guild_id,
    table_flag = e.currentTarget.dataset.table_flag,
    palt_type = e.currentTarget.dataset.palt_type,
    guild_name = e.currentTarget.dataset.guild_name,
    q_guild_id = e.currentTarget.dataset.q_guild_id,
    platfrom_id = e.currentTarget.dataset.platfrom_id;

    this.setData({
      guild_id: guild_id,
      table_flag: table_flag,
      guild_name: guild_name,
      q_guild_id: q_guild_id,
      platfrom_id: platfrom_id,
      // detail:true,
    })
    if (this.data.platfrom_id) {
      wx.navigateTo({
        url: '../anchorBind/anchorBind?detail=true&&guild_id=' + guild_id + '&&table_flag=' + table_flag + '&&palt_type=' + palt_type + '&&guild_name=' + guild_name + '&&platfrom_id=' + platfrom_id,
      })
    } else {
      wx.navigateTo({
        url: '../anchorBind/anchorBind?detail=true&&guild_id=' + guild_id + '&&table_flag=' + table_flag + '&&palt_type=' + palt_type + '&&guild_name=' + guild_name,
      })
    }
  },
  loadMore: function() {
    var that = this;
    console.log(this.data.anchors.length)
    // if (this.data.anchors.length%10!=0){
    if (this.data.anchors.length < 10) {
      this.setData({
        loadName: "暂无更多",
      })
      return false;
    } else {
      this.setData({
        loadName: "查看更多",
        count: this.data.count + 1,
      })
    }

    // console.log(this.data.count)

    if (this.data.guild_id) {
      if (this.data.palt_type == "1") {

        wx.request({
          url: app.globalData.url + 'api/mine/guild/anchor/list',
          method: "POST",
          data: {
            token: app.globalData.token,
            type: this.data.palt_type ? "1" : "2",
            guild_id: this.data.guild_id,
            table_flag: this.data.table_flag,
            page: this.data.count,
          },
          success: function(e) {
            console.log(e)
            let anchors_new = e.data.data.anchors.data;
            let anchors = (that.data.anchors).push(...anchors_new)
            that.setData({
              anchors: anchors,
              pages: e.data.data.anchors.current_page,
            })
            // console.log(that.data.anchors)

            if (e.data.data.anchors.last_page==that.data.count) {
              that.setData({
                loadName: "暂无更多",
              })
              return false;
            } else {
              that.setData({
                loadName: "查看更多",
                count: that.data.count + 1,
              })
            }
          }
        })
      } else {
        // console.log(11)
        // console.log(21321321321321)
        wx.request({
          url: app.globalData.url + 'api/mine/guild/anchor/list',
          method: "POST",
          data: {
            token: app.globalData.token,
            type: "2",
            guild_id: this.data.guild_id,
            table_flag: this.data.table_flag,
            page: this.data.count,
          },
          success: function(e) {
            console.log(e)
            var anchors_new = e.data.data.quick_anchors.data;
            var anchors = that.data.anchors
            console.log(anchors_new)
            anchors.push(...anchors_new)
            // console.log('after+' + anchors)
            that.setData({
              anchors: anchors,
              pages: e.data.data.anchors.current_page,
            })
            // console.log(that.data.anchors)

            if (e.data.data.anchors.last_page == that.data.count) {
              that.setData({
                loadName: "暂无更多",
              })
              return false;
            } else {
              that.setData({
                loadName: "查看更多",
                count: that.data.count + 1,
              })
            }
          }
        })
      }

    }

  },
  // 获取主播认证列表
  anchorList(){
    let that=this;
    wx.request({
      url: app.globalData.url + 'api/mine/guild/anchor/manage',
      method: "POST",
      data: {
        token: app.globalData.token,
      },
      success: (e) => {
        console.log(e);
        let anc_guilds = e.data.data.guilds;
        let anc_quick = e.data.data.quick_guilds;
        this.setData({
          anc_guilds: anc_guilds,
          anc_quick: anc_quick,
        })
        // console.log(anc_guilds)
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    let that = this;
    console.log(options)
    if (options.detail) {
      this.setData({
        detail: options.detail,
        guild_id: options.guild_id,
        palt_type: options.palt_type,
        table_flag: options.table_flag,
        guild_name: options.guild_name,
        q_guild_id: options.q_guild_id,
        platfrom_id: options.platfrom_id,
      })
      wx.setNavigationBarTitle({
        title: "主播列表", //页面标题为路由参数
      })
    }
    //设置主播身份
    this.setData({
      role: app.register_info.register_role
    })
    if (this.data.role == "2") {
      wx.request({
        url: app.globalData.url + 'api/mine/anchor/list',
        method: "POST",
        data: {
          token: app.globalData.token,
        },
        success: (e) => {
          console.log(e.data.data.anchor)
          this.setData({
            bind_anchor: e.data.data.anchor,
          })
        }
      })
    }


    if (options.guild_id) {
      if (options.palt_type == "1") {
        wx.request({
          url: app.globalData.url + 'api/mine/guild/anchor/list',
          method: "POST",
          data: {
            token: app.globalData.token,
            type: this.data.palt_type ? "1" : "2",
            guild_id: this.data.guild_id,
            table_flag: this.data.table_flag,
            page: "1",
          },
          success: function(e) {
            console.log(e.data.data.anchors)
            let anchors = e.data.data.anchors.data,
              anchorsAll = e.data.data.anchors;
            that.setData({
              anchors: anchors,
              pages: e.data.data.anchors.current_page,
            })
            console.log(that.data.anchorsAll)

            if (e.data.data.anchors.last_page == that.data.count) {
              that.setData({
                loadName: "暂无更多",
              })
              return false;
            } else {
              that.setData({
                loadName: "查看更多",
                count: that.data.count + 1,
              })
            }
          }
        })
      } else {
        // console.log(11)
        wx.request({
          url: app.globalData.url + 'api/mine/guild/anchor/list',
          method: "POST",
          data: {
            token: app.globalData.token,
            type: "2",
            guild_id: this.data.guild_id,
            table_flag: this.data.table_flag,
            page: "1",
          },
          success: function(e) {
            console.log(e)
            let anchors = e.data.data.quick_anchors.data;
            that.setData({
              anchors: anchors,
              pages: e.data.data.anchors.current_page,
            })
            // console.log(that.data.anchors)

            if (e.data.data.anchors.last_page == that.data.count) {
              that.setData({
                loadName: "暂无更多",
              })
              return false;
            } else {
              that.setData({
                loadName: "查看更多",
                count: that.data.count + 1,
              })
            }
          }
        })
      }

    }

    this.anchorList()




  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})